/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =====================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =====================================================
 *   $Id: Grid.java 598 2010-07-26 17:06:34Z Harry $
 *   $LastChangedDate: 2010-07-26 17:06:34 +0000 (Mon, 26 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.entity;

import java.io.Serializable;

/**
 *
 * @author Harry Nguyen
 */
public class Grid implements Serializable {

    /**
     *
     */
    protected Cell[][] cellGrid;
    /**
     *
     */
    protected int rows;
    /**
     *
     */
    protected int columns;

    /**
     *
     */
    public Grid() {
    }

    /**
     * Construct the Grid object with the number rows and columns
     *
     * @param  rows  the number of rows the grid will have
     * @param  columns  the number of columns the grid will have
     */
    public Grid(int rows, int columns) {
        cellGrid = new Cell[rows][columns];
        this.rows = rows;
        this.columns = columns;
    }

    /**
     * Returns the number of rows
     *
     * @return      number of rows
     */
    public int getRows() {
        return rows;
    }

    /**
     * Returns the number of columns the grid will have
     *
     * @return      the number of columns
     */
    public int getColumns() {
        return columns;
    }

    /**
     * Returns the starting gold
     *
     * @param  x  the x position of a cell
     * @param  y  the y position of a cell
     * @return      the cell located at that position
     */
    public Cell getCellAt(int x, int y) {
        if (x < 0 || x >= cellGrid.length) {
            return null;
        }

        if (y < 0 || y >= cellGrid[x].length) {
            return null;
        }

        return cellGrid[x][y];
    }

    /**
     * Sets a Cell object at a specific x and y position
     *
     * @param  cell  the Cell object to set
     * @param  x  the x position of the cell
     * @param  y  the y position of the cell
     */
    public void setCellAt(Cell cell, int x, int y) {
        if (x < 0 || x >= cellGrid.length) {
            return;
        }

        if (y < 0 || y >= cellGrid[x].length) {
            return;
        }

        cellGrid[x][y] = cell;
    }

    /**
     * Returns if there is a cell at the specific x and y position
     *
     * @param  x  the x position of the cell
     * @param  y  the y position of the cell
     * @return      true if there is a cell located in that position
     */
    public boolean hasCellAt(int x, int y) {
        if (x < 0 || x >= cellGrid.length) {
            return false;
        }

        if (y < 0 || y >= cellGrid[x].length) {
            return false;
        }

        return cellGrid[x][y] != null;
    }
}
